package processor;

import model.EquityPledge;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Selectable;
import util.MyString;
import util.Mytime;
import util.addTargetRequest;

import java.util.ArrayList;
import java.util.List;

//import static processor.LoginProcessor.isRedirectedToLoginPage;


public class EquityPledgeProcessor {
    public static void processEquityPledge(Page page) {
//        if(isRedirectedToLoginPage(page)){
//            return;
//        }
        //如果有股权质押
        List<Selectable> nodes = page.getHtml().xpath("//section[@id='pledgeList']//tr").nodes();
        List<EquityPledge> equityPledges = new ArrayList<EquityPledge>();
        if(nodes.size()>1){
            for(int i = 1;i<nodes.size();i++){
                List<Selectable>  tr_node = nodes.get(i).xpath("/tr//td").nodes();
                EquityPledge equityPledge = new EquityPledge();
                equityPledge.setRegister_id(tr_node.get(1).xpath("/td/allText()").toString());
                try{
                    equityPledge.setPledgor_unique(tr_node.get(2).xpath("/td//a/@href").toString()
                            .replace("firm_","")
                            .replace(".html","")
                            .replace("/",""));
                    if (!equityPledge.getPledgor_unique().startsWith("pl_")){
                        //如果当前优先级大于边界优先级 抓取下一层级股权出质关系
                        if (page.getRequest().getPriority()-1>=Integer.valueOf(page.getRequest().getExtra("InitialPriority").toString())
                                -Integer.valueOf(page.getRequest().getExtra("MinPledgePriority").toString())){
                            addTargetRequest.add(page,
                                    "https://www.qichacha.com/firm_"+equityPledge.getPledgor_unique()+".html#base",
                                    equityPledge.getPledgor_unique(),
                                    page.getRequest().getPriority()-1);
                        }

                    }
                }catch (Exception e){

                }
                equityPledge.setPledgor_name(MyString.Replace(tr_node.get(2).xpath("/td/allText()").toString()));
                try {
                    equityPledge.setPledgee_unique(tr_node.get(3).xpath("/td//a/@href").toString()
                            .replace("firm_","")
                            .replace(".html","")
                            .replace("/",""));
                    if (!equityPledge.getPledgee_unique().startsWith("pl_")){
                        //如果当前优先级大于边界优先级 抓取下一层级股权出质关系
                        if (page.getRequest().getPriority()-1>=Integer.valueOf(page.getRequest().getExtra("InitialPriority").toString())
                                -Integer.valueOf(page.getRequest().getExtra("MinPledgePriority").toString())){
                            addTargetRequest.add(page,
                                    "https://www.qichacha.com/firm_"+equityPledge.getPledgee_unique()+".html#base",
                                    equityPledge.getPledgee_unique(),
                                    page.getRequest().getPriority()-1);
                        }

                    }
                }catch (Exception e){

                }

                equityPledge.setPledgee_name(MyString.Replace(tr_node.get(3).xpath("/td/allText()").toString()));
                equityPledge.setStock_number(tr_node.get(4).xpath("/td/allText()").toString());
                equityPledge.setRegister_date(tr_node.get(5).xpath("/td/allText()").toString());
                equityPledge.setStatus(tr_node.get(6).xpath("/td/allText()").toString());
                equityPledge.setCreatedTime(Mytime.getTime());
                equityPledges.add(equityPledge);


            }
        }
        page.putField("EquityPledge",equityPledges);
    }

}
